package com.virtualtrainer_01.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.virtualtrainer_01.model.Group;
import com.virtualtrainer_01.model.Model;
import com.virtualtrainer_01.util.NameList;

public class GroupDB extends SQLiteDB {
	public static final String DATABASE_TABLE = "group1";
	public static final String KEY_ID = "_id";
	public static final String KEY_NAME = "name";
	public static final String KEY_TYPE = "type";
	public static final String KEY_DESCRIPTION = "description";

	public GroupDB(Context c) {
		super(c, DATABASE_TABLE);
	}

	@Override
	public String getData() {

		String[] columns = new String[] { KEY_ID, KEY_NAME, KEY_TYPE, KEY_DESCRIPTION };
		Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
		String result = "";

		int iRow = c.getColumnIndex(KEY_ID);
		int iName = c.getColumnIndex(KEY_NAME);
		int iType = c.getColumnIndex(KEY_TYPE);
		int iDescription = c.getColumnIndex(KEY_DESCRIPTION);

		for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext())
			result = result + c.getString(iRow) + " " + c.getString(iName) + " " + c.getString(iType) + " " + c.getString(iDescription) + "\n";
		return result;
	}

	@Override
	public long createEntry(Model entry) {
		Group g = (Group) entry;
		ContentValues cv = new ContentValues();
		cv.put(KEY_NAME, g.getName());
		cv.put(KEY_TYPE, g.getType());
		cv.put(KEY_DESCRIPTION, g.getDescription());

		return ourDatabase.insert(DATABASE_TABLE, null, cv);
	}

	public Group getGroup(String name) {
		String sql = "SELECT * FROM " + DATABASE_TABLE + " t where t.name=" + "'" + name + "';";
		Cursor c = ourDatabase.rawQuery(sql, null);

		int iName = c.getColumnIndex(KEY_NAME);
		int iType = c.getColumnIndex(KEY_TYPE);
		int iDescription = c.getColumnIndex(KEY_DESCRIPTION);

		Group grupa = new Group();

		for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
			grupa = new Group();
			grupa.setName(c.getString(iName));
			grupa.setDescription(c.getString(iDescription));
			grupa.setType(c.getString(iType));
		}
		return grupa;
	}

	public String[] getGroupNames() {
		String sql = "SELECT t." + KEY_NAME + " FROM " + DATABASE_TABLE + " t;";
		Cursor c = ourDatabase.rawQuery(sql, null);
		int iName = c.getColumnIndex(KEY_NAME);
		List<String> lista = new ArrayList<String>();
		for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext())
			lista.add(c.getString(iName));
		String[] list = new String[lista.size()];
		for (int i = 0; i < list.length; i++)
			list[i] = lista.get(i);
		return list;
	}

	public Group getGroup(int id) {
		String sql = "SELECT * FROM " + DATABASE_TABLE + " t where t." + KEY_ID + "=" + "'" + id + "';";
		Cursor c = ourDatabase.rawQuery(sql, null);

		int iName = c.getColumnIndex(KEY_NAME);
		int iType = c.getColumnIndex(KEY_TYPE);
		int iDescription = c.getColumnIndex(KEY_DESCRIPTION);

		Group ex = new Group();
		for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
			ex.setName(c.getString(iName));
			ex.setDescription(c.getString(iDescription));
			ex.setType(c.getString(iType));
		}
		return ex;
	}

	public NameList<Group> getGroupList() {
		String sql = "SELECT * FROM " + DATABASE_TABLE + " t;";
		Cursor c = ourDatabase.rawQuery(sql, null);

		int iName = c.getColumnIndex(KEY_NAME);
		int iType = c.getColumnIndex(KEY_TYPE);
		int iDescription = c.getColumnIndex(KEY_DESCRIPTION);

		NameList<Group> grList = new NameList<Group>();

		for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
			Group ex = new Group();
			ex.setName(c.getString(iName));
			ex.setDescription(c.getString(iDescription));
			ex.setType(c.getString(iType));
			grList.add(ex);
		}
		return grList;
	}
}
